package yu.liu.personal.app.myapplication.components.views;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import yu.liu.personal.app.myapplication.R;
import yu.liu.personal.app.myapplication.components.PopupCollection;
import yu.liu.personal.app.myapplication.models.AVGItem;

/**
 * Created by yu.liu on 2016/9/29.
 */

public class CalculatorPanelPopupWindow extends PopupCollection {

    @BindView(R.id.tvCodeCalPop)
    TextView tvCodeCalPop;

    @BindView(R.id.tvNameCalPop)
    TextView tvNameCalPop;

    @BindView(R.id.tvTypeCalPop)
    TextView tvTypeCalPop;

    @BindView(R.id.tvStarCalPop)
    TextView tvStarCalPop;

    @BindView(R.id.tvHPCalPop)
    TextView tvHPCalPop;

    @BindView(R.id.tvArmorCalPop)
    TextView tvArmorCalPop;

    @BindView(R.id.tvShieldCalPop)
    TextView tvShieldCalPop;

    @BindView(R.id.tvResistCalPop)
    TextView tvResistCalPop;

    @BindView(R.id.tvAtkCalPop)
    TextView tvAtkCalPop;

    @BindView(R.id.tvScoutCalPop)
    TextView tvScoutCalPop;

    @BindView(R.id.tvSpdCalPop)
    TextView tvSpdCalPop;

    @BindView(R.id.editLevelCalPop)
    EditText editLevelCalPop;

    @BindView(R.id.editEnhanceCalPop)
    EditText editEnhanceCalPop;

    @BindView(R.id.btnProceedCalPop)
    View btnProceedCalPop;

    @BindView(R.id.btnCancelCalPop)
    View btnCancelCalPop;

    private AVGItem item;

    private int resultHP;
    private int resultArmor;
    private int resultShield;
    private int resultResist;
    private int resultAtk;
    private int resultScout;
    private int resultSpd;

    public CalculatorPanelPopupWindow(Context context) {
        super(context);
    }

    @Override
    public boolean showPopupWindow() {
        if(anchorView != null && windowView != null) {
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            setContentView(windowView);
//            showAsDropDown(anchorView);
            showAtLocation(anchorView, Gravity.NO_GRAVITY, 0, 0);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void loadViews() {
        ButterKnife.bind(this, windowView);
    }

    @Override
    public void loadMembers() {

    }

    @Override
    public void postSetup() {
        btnProceedCalPop.setOnClickListener(new CalculateClickListener());
        btnCancelCalPop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    @Override
    public void updateViews() {
        tvCodeCalPop.setText("编号:"+item.id);
        tvNameCalPop.setText(item.name);
        tvTypeCalPop.setText(item.type);
        tvStarCalPop.setText(String.valueOf(item.stars)+"星");
        tvHPCalPop.setText("耐久:"+String.valueOf(resultHP));
        tvArmorCalPop.setText("装甲:"+String.valueOf(resultArmor));
        tvShieldCalPop.setText("护盾:"+String.valueOf(resultShield));
        tvResistCalPop.setText("抵抗:"+String.valueOf(resultResist));
        tvAtkCalPop.setText("火力:"+String.valueOf(resultAtk));
        tvScoutCalPop.setText("侦察:"+String.valueOf(resultScout));
        tvSpdCalPop.setText("机动:"+String.valueOf(resultSpd));
    }

    public void setItem(AVGItem item) {
        this.item = item;
        if (this.item != null) {
            resultHP = this.item.hp;
            resultArmor = this.item.armor;
            resultShield = this.item.shield;
            resultResist = this.item.resist;
            resultAtk = this.item.atk;
            resultScout = this.item.scout;
            resultSpd = this.item.spd;
        }
    }

    private void calculateTheValues(int level, int enhance) {
        if (this.item != null) {
            resultHP = (int) Math.ceil(((level + enhance + 11) / 12f) * this.item.hp);
            resultArmor = (int) Math
                    .ceil(((level + enhance + 11) / 12f) * this.item.armor);
            resultShield = (int) Math
                    .ceil(((level + enhance + 11) / 12f) * this.item.shield);
            resultResist = (int) Math
                    .ceil(((level + enhance + 11) / 12f) * this.item.resist);
            resultAtk = (int) Math.ceil(((level + enhance + 11) / 12f) * this.item.atk);
            resultScout = (int) Math
                    .ceil(((level + enhance + 11) / 12f) * this.item.scout);
            resultSpd = (int) Math.ceil(((level + enhance + 11) / 12f) * this.item.spd);
        }
    }

    private class CalculateClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            String levelStr = editLevelCalPop.getText().toString();
            String enhanceStr = editEnhanceCalPop.getText().toString();
            try {
                if (levelStr.length() == 0) {
                    Toast.makeText(resContext, "请输入等级...", Toast.LENGTH_LONG).show();
                } else {
                    int level = Integer.parseInt(levelStr);
                    int enhance = 0;
                    if (enhanceStr.length() > 0) {
                        enhance = Integer.parseInt(enhanceStr);
                    }
                    if (level > 0 && level <= 65 && enhance >= 0 && enhance <= 65) {
                        calculateTheValues(level, enhance);
                        updateViews();
                    } else {
                        Toast.makeText(resContext, "请输入合适的数值...", Toast.LENGTH_LONG)
                                .show();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
